package com.adingxiong.quickstart.project.controller;


import com.adingxiong.quickstart.common.bean.MailBean;
import com.adingxiong.quickstart.project.entity.EmailLog;
import com.adingxiong.quickstart.project.service.IEmailLogService;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.adingxiong.quickstart.util.MailUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import liquibase.pro.packaged.A;
import liquibase.pro.packaged.E;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * <p>
 * 邮箱记录 前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-05-28
 */
@RestController
@RequestMapping("/email-log")
@Api(tags = "邮箱管理(web)")
public class EmailLogController {

    @Autowired
    private MailUtils mailUtils;

    @Autowired
    private IEmailLogService emailLogService;


    @PostMapping("/sendMsg")
    @ApiOperation(value = "定向发送邮箱,并保存")
    public Result sendEmail(
            @ApiParam(value = "发送人姓名") @RequestParam(value = "sendName" ,required = false) String sendName,
            @ApiParam(value = "发送人邮箱",required = true) @RequestParam(value = "email" ,required = true) String email,
            @ApiParam(value = "邮箱内容",required = true) @RequestParam(value = "content", required = true) String content
    ){
        EmailLog emailLog = new EmailLog();
        if(!StringUtils.isEmpty(sendName)){
            emailLog.setSendName(sendName);
        }
        emailLog.setEmail(email);
        emailLog.setContent(content);

        LocalDateTime time = LocalDateTime.now();
        emailLog.setSendTime(time);

        MailBean bean = new MailBean();
        bean.setRecipient(email);
        bean.setContent(content);
        bean.setSubject("网站来信");
        try {
            mailUtils.sendHTMLMail(bean);

            boolean insert = emailLogService.save(emailLog);
            return new Result(insert ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(), insert ? "发送成功" : "发送失败", insert);
        } catch (Exception e){
            return new Result(RetCode.SYSTEM_ERROR.getCode(),RetCode.SYSTEM_ERROR.getMsg(),false);
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询邮箱记录")
    public Result<Page<EmailLog>> getList(
            @ApiParam(value = "关键字") @RequestParam(value = "keywords" ,required = false) String keywords,
            @ApiParam(value = "当前页码") @RequestParam(value = "current" ,required = false,defaultValue = "1") Integer current,
            @ApiParam(value = "每页条数") @RequestParam(value = "pageSize" ,required = false,defaultValue = "10") Integer pageSize
    ){
        QueryWrapper<EmailLog> query  = new QueryWrapper<>();
        Page<EmailLog> page  =new Page<>();
        query.orderByDesc("send_time");
        page.setSize(pageSize);
        page.setCurrent(current);
        /*query.lambda().like(!StringUtils.isEmpty(keywords),EmailLog::getContent,keywords);*/
        if(!StringUtils.isEmpty(keywords)){
            query.like("content" ,keywords).or().like("send_name",keywords).or().like("email",keywords);
        }
        Page<EmailLog> res = emailLogService.page(page,query);
        return new Result<>(RetCode.SUCCESS.getCode(),"查询成功",res);
    }

    @GetMapping("/delByIds")
    @ApiOperation(value = "根据主键批量删除")
    public Result dels(
            @ApiParam(value = "主键标识",required = true) @RequestParam(value = "ids" ,required = true)  List<Integer> ids
    ){
        if(StringUtils.isEmpty(ids) && ids.size()== 0){
           return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        boolean del = emailLogService.removeByIds(ids);
        return new Result(del ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(), del ? "批量操作成功" : "批量操作失败", del);
    }


    @PostMapping("/update")
    @ApiOperation(value = "根据id进行修改")
    public Result updateByID (EmailLog emailLog){
        if(emailLog == null){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        boolean update  = emailLogService.updateById(emailLog);
        return new Result(update ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(), update ? "修改成功" : "修改失败", update);


    }
}

